<?php

class OpdrachtForm extends Form
{
	const L_FIELD_REQUIRED = 'Dit veld is verplicht';
	
	protected $bedrijven = null;
	
	public function __construct( $data = array() )
	{
		parent::__construct( );
		
		if( isset( $data['id'] ) )
		{
			$this->addChild( new InputHidden( 'id', $data['id'] ) );
		}
		
		$fieldset = new FieldSet( '' );
		
		$field = new InputText( 'name', Opdracht::getField('name'), isset( $data[ 'name' ] ) ? $data[ 'name' ] : '', 'name' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		
		$this->bedrijven = new SelectOneChoice( 'bedrijf_id', 'Bedrijf', '', '' );
		$fieldset->addChild( $this->bedrijven );
		
		$field = new InputText( 'start_date', Opdracht::getField('start_date') . ' (DD-MM-JJJJ)', isset( $data[ 'start_date' ] ) ? $data[ 'start_date' ] : '', 'start_date' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$field->getValidator()->compareMatch('/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{2,4}$/', 'De datum moet in in DD-MM-JJJJ formaat worden opgegeven.');
		$fieldset->addChild( $field );
		
		$field = new InputText( 'end_date', Opdracht::getField('end_date') . ' (DD-MM-JJJJ)', isset( $data[ 'end_date' ] ) ? $data[ 'end_date' ] : '', 'end_date' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$field->getValidator()->compareMatch('/^[0-9]{1,2}-[0-9]{1,2}-[0-9]{2,4}$/', 'De datum moet in in DD-MM-JJJJ formaat worden opgegeven.');
		$fieldset->addChild( $field );
		
		$field = new InputText( 'length_weeks', Opdracht::getField('length_weeks'), isset( $data[ 'length_weeks' ] ) ? $data[ 'length_weeks' ] : '', 'length_weeks' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		
		$field = new InputText( 'pay', Opdracht::getField('pay'), isset( $data[ 'pay' ] ) ? $data[ 'pay' ] : '', 'pay' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		
		$field = new InputText( 'afdeling', Opdracht::getField('afdeling'), isset( $data[ 'afdeling' ] ) ? $data[ 'afdeling' ] : '', 'afdeling' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		
		$field = new InputText( 'days_off', Opdracht::getField('days_off'), isset( $data[ 'days_off' ] ) ? $data[ 'days_off' ] : '', 'days_off' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		$this->addChild($fieldset);
		
		$fieldset = new FieldSet( 'Stageopdracht gegevens' );
	
		$field = new InputText( 'description', Opdracht::getField('description'), isset( $data[ 'description' ] ) ? $data[ 'description' ] : '', 'description' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		
		$field = new InputText( 'other_stuff', Opdracht::getField('other_stuff'), isset( $data[ 'other_stuff' ] ) ? $data[ 'other_stuff' ] : '', 'other_stuff' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		
		$field = new InputText( 'supervisor_info', Opdracht::getField('supervisor_info'), isset( $data[ 'supervisor_info' ] ) ? $data[ 'supervisor_info' ] : '', 'supervisor_info' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		
		$field = new Checkbox( 'klw_opdr', Opdracht::getField('klw_opdr'), isset( $data['klw_opdr'] ) );
		$fieldset->addChild( $field );
		
		$field = new Checkbox( 'werkplek', Opdracht::getField('werkplek'), isset( $data['werkplek'] ) );
		$fieldset->addChild( $field );
			
		$field = new InputText( 'status', Opdracht::getField('status'), isset( $data[ 'status' ] ) ? $data[ 'status' ] : '', 'status' );
		$field->getValidator()->required( self::L_FIELD_REQUIRED );
		$fieldset->addChild( $field );
		$this->addChild($fieldset);
		$this->addChild( new SubmitButton( 'submit', 'Opslaan' ) );
	}
	
	public function setBedrijven( $bedrijven, $active )
	{
		foreach( $bedrijven as $bedrijf )
		{
			$this->bedrijven->addValue( $bedrijf->getSugarId(), $bedrijf->name );
		}
		
		if( !empty( $active ) )
		{
			$this->bedrijven->setSelected( $active );
		}
	}
}
